<template>
  <div class="container">
    <button
      type="button"
      @click="
        () => {
          showMenu = !showMenu
          editor && editor.commands.focus()
        }
      "
    >
      Toggle menu
    </button>
    <div class="control-group">
      <label>
        <input type="checkbox" :checked="isEditable" @change="() => (isEditable = !isEditable)" />
        Editable
      </label>
    </div>

    <bubble-menu v-if="editor && showMenu" :editor="editor" :options="{ placement: 'bottom', offset: 8 }">
      <div class="bubble-menu">
        <button @click="editor.chain().focus().toggleBold().run()" :class="{ 'is-active': editor.isActive('bold') }">
          Bold
        </button>
        <button
          @click="editor.chain().focus().toggleItalic().run()"
          :class="{ 'is-active': editor.isActive('italic') }"
        >
          Italic
        </button>
        <button
          @click="editor.chain().focus().toggleStrike().run()"
          :class="{ 'is-active': editor.isActive('strike') }"
        >
          Strike
        </button>
      </div>
    </bubble-menu>

    <bubble-menu
      v-if="editor && showMenu"
      :editor="editor"
      :should-show="() => editor.isActive('bulletList') || editor.isActive('orderedList')"
      :get-referenced-virtual-element="getListVirtualElement"
      :options="{ placement: 'top-start', offset: 8 }"
    >
      <div class="bubble-menu">
        <button type="button" @click="toggleListType">Toggle list type</button>
      </div>
    </bubble-menu>

    <editor-content :editor="editor" />
  </div>
</template>

<script>
import { findParentNode, posToDOMRect } from '@tiptap/core'
import StarterKit from '@tiptap/starter-kit'
import { Editor, EditorContent } from '@tiptap/vue-3'
import { BubbleMenu } from '@tiptap/vue-3/menus'

export default {
  components: {
    EditorContent,
    BubbleMenu,
  },

  data() {
    return {
      editor: null,
      isEditable: true,
      showMenu: true,
    }
  },

  watch: {
    isEditable(value) {
      this.editor.setEditable(value)
    },
  },

  mounted() {
    this.editor = new Editor({
      extensions: [StarterKit],
      content: `
        <p>
          Hey, try to select some text here. There will popup a menu for selecting some inline styles. Remember: you have full control about content and styling of this menu.
        </p>
        <ul>
          <li>Select any item to display a global menu</li>
          <li>Item 1</li>
          <li>Item 2</li>
          <li>Item 3</li>
          <li>Item 4</li>
        </ul>
      `,
    })
  },

  methods: {
    getListVirtualElement() {
      const editor = this.editor
      const parentNode = findParentNode(node => node.type.name === 'bulletList' || node.type.name === 'orderedList')(
        editor.state.selection,
      )
      if (parentNode) {
        const domRect = posToDOMRect(editor.view, parentNode.start, parentNode.start + parentNode.node.nodeSize)
        return {
          getBoundingClientRect: () => domRect,
          getClientRects: () => [domRect],
        }
      }
      return null
    },
    toggleListType() {
      const editor = this.editor
      const chain = editor.chain().focus()
      if (editor.isActive('bulletList')) {
        chain.toggleOrderedList()
      } else {
        chain.toggleBulletList()
      }
      chain.run()
    },
  },

  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>

<style lang="scss">
/* Basic editor styles */
.tiptap {
  :first-child {
    margin-top: 0;
  }

  /* List styles */
  ul,
  ol {
    padding: 0 1rem;
    margin: 1.25rem 1rem 1.25rem 0.4rem;

    li p {
      margin-top: 0.25em;
      margin-bottom: 0.25em;
    }
  }

  /* Heading styles */
  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    line-height: 1.1;
    margin-top: 2.5rem;
    text-wrap: pretty;
  }

  h1,
  h2 {
    margin-top: 3.5rem;
    margin-bottom: 1.5rem;
  }

  h1 {
    font-size: 1.4rem;
  }

  h2 {
    font-size: 1.2rem;
  }

  h3 {
    font-size: 1.1rem;
  }

  h4,
  h5,
  h6 {
    font-size: 1rem;
  }

  /* Code and preformatted text styles */
  code {
    background-color: var(--purple-light);
    border-radius: 0.4rem;
    color: var(--black);
    font-size: 0.85rem;
    padding: 0.25em 0.3em;
  }

  pre {
    background: var(--black);
    border-radius: 0.5rem;
    color: var(--white);
    font-family: 'JetBrainsMono', monospace;
    margin: 1.5rem 0;
    padding: 0.75rem 1rem;

    code {
      background: none;
      color: inherit;
      font-size: 0.8rem;
      padding: 0;
    }
  }

  blockquote {
    border-left: 3px solid var(--gray-3);
    margin: 1.5rem 0;
    padding-left: 1rem;
  }

  hr {
    border: none;
    border-top: 1px solid var(--gray-2);
    margin: 2rem 0;
  }
}

/* Bubble menu */
.bubble-menu {
  background-color: var(--white);
  border: 1px solid var(--gray-1);
  border-radius: 0.7rem;
  box-shadow: var(--shadow);
  display: flex;
  padding: 0.2rem;

  button {
    background-color: unset;

    &:hover {
      background-color: var(--gray-3);
    }

    &.is-active {
      background-color: var(--purple);

      &:hover {
        background-color: var(--purple-contrast);
      }
    }
  }
}
</style>
